package ltd.followkba.page;

import android.view.View;

import com.kcbsdk.EventHandler;

import ltd.advskin.MSKIN;
import ltd.advskin.base.BaseInput;
import ltd.advskin.base.BaseList;
import ltd.advskin.base.BasePage;

import ltd.advskin.base.KcTypeListener;
import ltd.advskin.base.MPAGE;
import ltd.advskin.task.TaskFree;
import ltd.advskin.view.KcBtnProgress;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.W;
import ltd.followkba.task.App32;
import ltd.followkba.task.App33;
import ltd.followkba.task.BAppUser;
import ltd.followkba.task.LogTyp;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.mMyWorks;

public class EditTaskPage extends BaseList {
    private int gWorksType;

    private View kbtHeadEdit;
    private View tvNameEdit;
    private View kbtLoginEdit;
    private View tvAccountEdit;
    private View tvStatusEdit;
    private View kbtModifyEdit;
    private View kbtStepTime;
    private View tvStepLike;
    private View kbtCommentModify;
    private View tvCommentEdit;
    private View kbtCareModify;
    private View tvCareEdit;
    private View kbtGiftModify;
    private View tvGiftEdit;
    private View kbrCommentEdit;
    private View kbpOperation;
    private View kbtUploadEdit;
    private View tvMoneyEdit;
    private KcBtnProgress kbtPauseEdit;
//    private boolean isClose;
//    private boolean isPause;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;
    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_edit_task);
        if (mBaseInput != null) {                   // 可能仅仅需要刷新，未必是重新开始的
            MAPP.mBDbsAccount = mBaseInput.getInput(0);
            mBaseInput = null;                  // 输入参数的使命已经完成，记得及时销毁之
        }
        gWorksType = MAPP.mBDbsAccount.WorksType;
        mKcHeaderView = findViewById(R.id.khvEdit);
        String text = String.format("编辑任务%s", MAPP.mWorksName[gWorksType]);
//        mKcHeaderView.setTitle(text);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });
        kbtHeadEdit = findViewById(R.id.kbtHeadEdit);
        tvNameEdit = findViewById(R.id.tvNameEdit);
        kbtLoginEdit = findViewById(R.id.kbtLoginEdit);
        tvAccountEdit = findViewById(R.id.tvAccountEdit);
        tvStatusEdit = findViewById(R.id.tvStatusEdit);
        kbtModifyEdit = findViewById(R.id.kbtModifyEdit);
        kbtStepTime = findViewById(R.id.kbtStepTime);
        tvStepLike = findViewById(R.id.tvStepLike);
        kbtCommentModify = findViewById(R.id.kbtCommentModify);
        tvCommentEdit = findViewById(R.id.tvCommentEdit);
        kbtCareModify = findViewById(R.id.kbtCareModify);
        tvCareEdit = findViewById(R.id.tvCareEdit);
        kbtGiftModify = findViewById(R.id.kbtGiftModify);
        tvGiftEdit = findViewById(R.id.tvGiftEdit);
        kbrCommentEdit = findViewById(R.id.kbrCommentEdit);
        kbpOperation = findViewById(R.id.kbpOperation);
        kbtUploadEdit = findViewById(R.id.kbtUploadEdit);
        tvMoneyEdit = findViewById(R.id.tvMoneyEdit);
        kbtPauseEdit = findViewById(R.id.kbtPauseEdit);

        kbtHeadEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                       // 头像
                BAppUser bAppUser = new BAppUser();
                bAppUser.WorksType = MAPP.mBDbsAccount.WorksType;
                bAppUser.UserID = MAPP.mBDbsAccount.UserID;
                MAPP.startUserHomePage(bAppUser, false);
            }
        });
        kbtLoginEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                       // 登录
                LoginTaskPage loginTaskPage = new LoginTaskPage();
                MPAGE.startBasePage(loginTaskPage);
            }
        });
        kbtModifyEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 修改资料
                ModifyUserPage mBaseInput = new ModifyUserPage();
                MPAGE.startBasePage(mBaseInput);
            }
        });
        kbtStepTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 间隔点赞
                StepLikePage stepLikePage = new StepLikePage();
                MPAGE.startBasePage(stepLikePage);
            }
        });
        kbtCommentModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                       // 评论文字
                MAPP.startCommentTextPage(LogTyp.NORMAL_COMMENT);
            }
        });
        kbtCareModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 自动关注
                AutoCarePage autoCarePage = new AutoCarePage();
                autoCarePage.mBaseInput = new BaseInput(MAPP.mBDbsAccount);
                MPAGE.startBasePage(autoCarePage);
            }
        });
        kbtGiftModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 自动礼物
                AutoGiftPage autoGiftPage = new AutoGiftPage();
                autoGiftPage.mBaseInput = new BaseInput(MAPP.mBDbsAccount);
                MPAGE.startBasePage(autoGiftPage);
            }
        });
        kbrCommentEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 查看LOG
                DoneLogPage doneLogPage = new DoneLogPage();
                doneLogPage.mBaseInput = new BaseInput(MAPP.mBDbsAccount);
                MPAGE.startBasePage(doneLogPage);
            }
        });
        kbpOperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                           // 手动操作
                OperationPage operationPage = new OperationPage();
                operationPage.mBaseInput = new BaseInput(MAPP.mBDbsAccount);
                MPAGE.startBasePage(operationPage);
            }
        });
        kbtUploadEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 查看上传计划
                MAPP.startUploadListPage();
            }
        });
        findViewById(R.id.kbtCleanEdit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 清除缓冲

            }
        });
        kbtPauseEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 暂停后台


            }
        });
        findViewById(R.id.kbtDeleteEdit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 删除当前任务
//                BRunRam bRunRam = RunUtils.getTaskEach(MAPP.mBDbsAccount);
//                if (bRunRam != null) {
//                    bRunRam.mBDbsAccount.IsDelete = true;
//                    bRunRam.SetStop = true;
//                }
                MDBS.delBDbsAccount(MAPP.mBDbsAccount);
                MTOAST(String.format("已经删除账号%s", MAPP.mBDbsAccount.AccountName));
                setPageResult(MAPP.mBDbsAccount);
                onFinish();
            }
        });
        if (MAPP.mBDbsAccount.WorksType == 33 || MAPP.mBDbsAccount.WorksType == 49){
//            MAPP.mAccount33 = new App33(MAPP.mBDbsAccount);
        }
        MLOG("打开E");
        setTaskPause(true);
    }


    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
//                        showInfo();
                    }
                }
            }
        });
        MLOG(String.format("AddTaskPageE %s", mMyWorks.mEventHandler));
        showInfo();
    }


    @Override
    public void onPageUpdate(BasePage basePage) {    // 更新显示页面数据，用于刷新显示 basePage返回上面的页面，可以使用getPageResult()获得返回的内容
        Object object = getPageResult(basePage);
        if (object != null && basePage instanceof LoginTaskPage){
//            setPageResult(MAPP.mBDbsAccount);
            onFinish();                         // 登录成功关闭
            return;
        }
        onInitData();
    }
    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        MTOAST("正在退出...");
//        if (!isClose) {
//            isClose = true;
            setTaskPause(false);
//        }
    }
    public void setTaskPause(boolean isPause){
        new TaskFree(isPause, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                boolean isPause = (Boolean)object;
                if (isThread) {
                    MAPP.setTaskPause(isPause);
                    return;
                }
                if (isPause) {
                    MTOAST("已暂停所有任务", 3000);
                }else {
//                    MAPP.mAccount33 = null;
//                    MAPP.mKcApp32 = null;
                    MAPP.mSearch = null;
                    setClose();
                    MTOAST("关闭页面及运行所有任务", 3000);
                }
            }
        });
    }


    private void showInfo() {
        MAPP.showUserHead(MAPP.mBDbsAccount.HeadFile, MAPP.mBDbsAccount, kbtHeadEdit);    // kbtHeadEdit

        MAPP.showUserName(MAPP.mBDbsAccount.UserName, MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount, tvNameEdit); // tvNameEdit
        MSKIN.setText(MAPP.mBDbsAccount.AccountName, kbtLoginEdit);
//        String app = MSKIN.getStringArray(R.array.task_short_name)[MAPP.mBDbsAccount.TabType];
        MSKIN.setText(String.format("登录%s",  MAPP.mWorksName[MAPP.mBDbsAccount.WorksType]), kbtLoginEdit);
        String text = String.format("%s,%s", MAPP.mBDbsAccount.AccountName, MAPP.mBDbsAccount.Password);
        MSKIN.setText(text, tvAccountEdit);
        MSKIN.setText(MAPP.mBDbsAccount.Status, tvStatusEdit);      // tvStatusEdit


        text = String.format("间隔时间%s秒", MAPP.mBDbsAccount.SetConfig[0]);     // [0]间隔时间设置
        MSKIN.setText(text, tvStepLike);        // tvStepLike

        String[] tabs = MSKIN.getStringArray(R.array.edit_text_type);
        int length1 = MAPP.mBDbsAccount.TextComment[W.TEXT_NORMAL].split("\n").length;
        int length2 = MAPP.mBDbsAccount.TextComment[W.TEXT_NEW].split("\n").length;
        int length3 = MAPP.mBDbsAccount.TextComment[W.TEXT_OLD].split("\n").length;
        text = String.format("%s:%s %s:%s %s:%s", tabs[0], length1, tabs[1], length2, tabs[2], length2);
//        text = String.format("%s:%s %s:%s %s:%s", tabs[0], MAPP.mBDbsAccount.NormalComment.length, tabs[1], MAPP.mBDbsAccount.NewComment.length, tabs[2], MAPP.mBDbsAccount.OldComment.length);
        MSKIN.setText(text, tvCommentEdit);     // tvCommentEdit

        if (MAPP.mBDbsAccount.CareSet == null){
            text = "还没有设置自动关注";
        }else {
            StringBuffer stringBuffer = new StringBuffer();
            if (MAPP.mBDbsAccount.CareSet[0] >= 0){
                stringBuffer.append(String.format("粉丝多于%s ", MAPP.mBDbsAccount.CareSet[0]));
            }
            if (MAPP.mBDbsAccount.CareSet[1] >= 0){
                stringBuffer.append(String.format("粉丝少于%s ", MAPP.mBDbsAccount.CareSet[1]));
            }
            if (MAPP.mBDbsAccount.CareSet[2] >= 0){
                stringBuffer.append(String.format("作品多于%s ", MAPP.mBDbsAccount.CareSet[2]));
            }
            if (MAPP.mBDbsAccount.CareSet[3] >= 0){
                stringBuffer.append(String.format("作品少于%s ", MAPP.mBDbsAccount.CareSet[3]));
            }
            length1 = MAPP.mBDbsAccount.TextComment[W.TEXT_CARE].split("\n").length;
            stringBuffer.append(String.format("  文字%s", length1));
            text = stringBuffer.toString();
        }
        MSKIN.setText(text, tvCareEdit);        // tvCareEdit
        if (MAPP.mBDbsAccount.GiftSet == null){
            text = "还没有设置自动送礼物";
        }else {
            StringBuffer stringBuffer = new StringBuffer();
            if (MAPP.mBDbsAccount.GiftSet[0] >= 0){
                stringBuffer.append(String.format("粉丝多于%s ", MAPP.mBDbsAccount.GiftSet[0]));
            }
            if (MAPP.mBDbsAccount.GiftSet[1] >= 0){
                stringBuffer.append(String.format("粉丝少于%s ", MAPP.mBDbsAccount.GiftSet[1]));
            }
            if (MAPP.mBDbsAccount.GiftSet[2] >= 0){
                stringBuffer.append(String.format("作品多于%s ", MAPP.mBDbsAccount.GiftSet[2]));
            }
            if (MAPP.mBDbsAccount.GiftSet[3] >= 0){
                stringBuffer.append(String.format("作品少于%s ", MAPP.mBDbsAccount.GiftSet[3]));
            }
            length1 = MAPP.mBDbsAccount.TextComment[W.TEXT_CARE].split("\n").length;
            stringBuffer.append(String.format("  文字%s", length1));
            text = stringBuffer.toString();
        }
        MSKIN.setText(text, tvGiftEdit);        // tvGiftEdit

//        if (gUploadPlan > 0){
//            MSKIN.setText(String .format("查看上传计划(%s)", gUploadPlan), kbtUploadEdit);
//        }
        if (gWorksType < 32) {
//            BRunRam bRunRam = RunUtils.getTaskEach(MAPP.mBDbsAccount);
//            if (bRunRam != null && bRunRam.gGiftMoney != null) {               // 免费礼物 + 免费礼物上限 + 已使用积分 + 剩余积分
//                text = String.format("账号财富：免费礼物%s/%s 积分%s/%s", bRunRam.gGiftMoney[0], bRunRam.gGiftMoney[1], bRunRam.gGiftMoney[2], bRunRam.gGiftMoney[3]);
//            }else {
//                text = String.format("账号财富：还没有读取");
//            }
//            MSKIN.setText(text, tvMoneyEdit);
        }else {
            tvMoneyEdit.setVisibility(View.GONE);
        }
    }


}

